/**
 * 2018/9/4 星期二 09:35:10 Lynn Li created.
 */

package com.diver.flinkdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diver.flinkdemo.entity.xxt.School;
import com.diver.flinkdemo.mapper.mysqlMapper.SchoolMapper;
import com.diver.flinkdemo.service.ISchoolService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

//@Service
@Slf4j
public class SchoolService implements ISchoolService {


//    @Autowired
    private SchoolMapper mapper;
    private SqlSession session;
//
//    @Override
//    public IIbatisDataProvider<School, String> getModelDao() {
//        return this.schoolDao;
//    }
//
//    /**
//     * 学校列表
//     *
//     * @param params
//     * @param pageNumber
//     * @param pageSize
//     * @return
//     */
//    @Override
//    public PagedList<School> querySchoolList(Map<String, Object> params, int pageNumber, int pageSize) {
//        QueryParams queryParams = new QueryParams();
//        queryParams.addParameter("deleteFlag", 0);
//        if (params.get("schoolId") != null && StringUtils.isNotBlank(params.get("schoolId").toString())) {
//            queryParams.addParameter("id", params.get("schoolId").toString());
//        }
//        if (params.get("hasFence") != null) {
//            queryParams.addParameter("hasFence", params.get("hasFence"));
//        }
//        PagedList<School> list = queryPagedList(queryParams, pageNumber - 1, pageSize);
//        return list;
//    }
//
//    @Override
//    public List<String> querySchoolNames() {
//        return mapper.aueryNames();
//    }
//
//    @Override
//    public void saveSchoolByXY(School school, int type) {
//        if (type == 0){//0 新增;
//            school.setDeleteFlag(DeleteFlagEnum.UNDELETE.getType());
//            school.setCreateTime(System.currentTimeMillis());
//            school.setCreatePersion("xy");
//            doAddModel(school);
//            manageClient.doOperateChannel(school);//初始化渠道表
//        }else if (type == 1){//1 删除;
//            School sc = new School();
//            sc.setId(school.getId());
//            sc.setDeleteFlag(DeleteFlagEnum.DELETED.getType());
//            sc.setDeletePersion("xy");
//            sc.setDeleteTime(System.currentTimeMillis());
//            doUpdateSelectiveById(sc);
//        }else if (type == 2){//2 修改
//            school.setUpdatePersion("xy");
//            school.setUpdateTime(System.currentTimeMillis());
//            doUpdateSelectiveById(school);
//        }
//    }

    @Override
    public School queryById(String schoolId) {
//        QueryParams queryParams = new QueryParams();
//        queryParams.addParameter("deleteFlag", 0);
//        queryParams.addParameter("id", schoolId);
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(SchoolMapper.class);
            QueryWrapper<School> wrapper = new QueryWrapper<>();
            wrapper.eq("delete_flag",0);
            wrapper.eq("id",schoolId);
            List<School> schools = mapper.selectList(wrapper);
            if (schools!=null &&schools.size()>0){
                return schools.get(0);
            }
            return null;
        } finally {
            endOfSession(session);
        }
    }
    
    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
